package com.bjpowernode.shop.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.bjpowernode.shop.domain.SmsLog;
import com.bjpowernode.shop.domain.SmsMessage;
import com.bjpowernode.shop.mapper.SmsLogMapper;
import com.bjpowernode.shop.properties.AliyunProperties;
import com.google.gson.Gson;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class SmsUtils {

    /*    
        public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-beijing",
                "LTAI5tQLrPWoueXuxadE6C5p",
                "AW5fZ9qP9wbPC6c3wpOlJZWPUIWE2o");

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers("13556140939");// 接收短信的手机号码
        request.setSignName("吖白商城");// 短信签名名称
        request.setTemplateCode("SMS_276190055");// 短信模板的CODE
        request.setTemplateParam("{\"code\":6666}");// 短信模板变量对应的实际值

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    } */

    @Autowired
    private AliyunProperties aliyunProperties;

    @Autowired
    private SmsLogMapper smsLogMapper;

    public boolean sendSms(SmsMessage smsMessage) {
        DefaultProfile profile = DefaultProfile.getProfile(
                aliyunProperties.getRegionId(),
                aliyunProperties.getAccessKey(),
                aliyunProperties.getAccessSecret()
        );

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(smsMessage.getPhonenum());// 接收短信的手机号码
        request.setSignName(aliyunProperties.getSignName());// 短信签名名称
        request.setTemplateCode(aliyunProperties.getTemplateCode());// 短信模板的CODE
        request.setTemplateParam("{\"code\":" + smsMessage.getCode() + "}");// 短信模板变量对应的实际值

        try {
            // {"requestId":"D78E6D04-B6AF-53C4-B235-12EBD9B2203B","bizId":"160619480859884476^0","code":"OK","message":"OK"}
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            
            // 发送成功则添加SMSLog
            int insert = 0;
            if (response.getCode().equalsIgnoreCase("ok")){
                insert = smsLogMapper.insert(
                        SmsLog.builder()
                                .userId(smsMessage.getUserId())
                                .userPhone(smsMessage.getPhonenum())
                                .content(String.format(aliyunProperties.getTemplateContent(), smsMessage.getCode()))
                                .mobileCode(String.valueOf(smsMessage.getCode()))
                                .type(2)
                                .recDate(new Date())
                                .responseCode(response.getCode())
                                .status(1)
                                .build()
                );
            }
            if (insert > 0)
                return true;

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return false;
    }
}
